/*
 * @Author: yusha
 * @Date: 2023-06-13 15:33:41
 * @LastEditors: yusha
 * @LastEditTime: 2023-07-11 17:06:35
 * @Description: 接口
 */
import axios from 'axios';
import { message } from 'antd';
import { serviceConfig } from './swaggerServiceConfig';

export interface User {
    loginInfo: {
      userId: string;
      username: string;
    };
    userInfo: {};
    auth: never[];
    token: string;
  }
/** 接口前缀 */
const newConfig = { ...serviceConfig };
/** 获取token */
const getUserInLocal = () => {
    const usrStr = window.localStorage.getItem('user');
    if (!usrStr) return;
    try {
        const user = JSON.parse(usrStr).user as User;
        return user;
    } catch (e) {
        return undefined;
    }
};
const axiosOemPurchase = axios.create({
    baseURL: newConfig.baseURL,
    headers: { 'X-Authtoken': getUserInLocal()?.token }
});

// 返回拦截
axiosOemPurchase.interceptors.response.use(function (response) {
    if (response.data.code === 200 || response.data.code === '0' || response.data.code === 0 || 1 === 1 ) return response;
    message.error(response.data.message);
    return Promise.reject(response.data.message);
}, function (error) {
    message.error('请求错误：' + String(error));
    return Promise.reject(error);
});

/** 用户注册 */
export const signup = (params) => {
    return axiosOemPurchase({ url: '/user/signup', method: 'POST', params });
};

/** 用户充值 */
export const rechange = (params) => {
    return axiosOemPurchase({ url: '/user/rechange', method: 'POST', params });
};


/** 修改会员等级 */
export const changeMembershipLvl = (params) => {
    return axiosOemPurchase({ url: '/user/changeMembershipLvl', method: 'POST', params });
};

// 获取验证码
export const getCode = (params) => {
    return axiosOemPurchase({ url: '/user/getCode', method: 'GET', params });
};

// 查询list
export const getUsers = (params) => {
  return axiosOemPurchase({ url: '/test/baseData/list', method: 'POST', params });
};

